package com.spyx.acl.util;

import com.atyx.spyx.model.acl.Permission;

import java.util.ArrayList;
import java.util.List;

public class MyPermissionHelper {
    public static List<Permission> build(List<Permission> permissionList) {
            //1.创建一个集合，用来保存分级之后的菜单
            List<Permission> lastmenus=new ArrayList<>();
            //2.判断顶级菜单，并找到该菜单的子菜单
            for (Permission permission : permissionList) {
                if(permission.getPid()==0){  //顶级菜单的判断条件
                    permission.setLevel(1);//设置级别
                    lastmenus.add(find(permission,permissionList)); //设置permission的子菜单
                }
            }
            return lastmenus;
    }
    //在众多菜单中找到属于某个permission有父子关系的菜单集合
    public static Permission find(Permission permission ,List<Permission> permissionList){
        permission.setChildren(new ArrayList<>());
        for (Permission p1 : permissionList) {
            if(permission.getId().longValue()==p1.getPid().longValue()){
                //说明p1这个菜单属于permission的子菜单
                p1.setLevel(permission.getLevel()+1);
                permission.getChildren().add(find(p1,permissionList));//递归
            }
        }
        return permission;
    }
}
